package com.dwf.dominion;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class ActivityRuleEntry extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
        setContentView(R.layout.rule_entry_layout);
        
		Spinner spinner = (Spinner)this.findViewById(R.id.rule_question);  
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.rule_question, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spinner.setAdapter(adapter); 

    	OnItemSelectedListener itemListener = new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				Spinner my_spinner;
					if(position == 0) {
						my_spinner = (Spinner)findViewById(R.id.rule_qualifier2);
						my_spinner.setVisibility(View.VISIBLE);
						
						my_spinner = (Spinner)findViewById(R.id.rule_quantity2);
						my_spinner.setVisibility(View.VISIBLE);
						
						my_spinner = (Spinner)findViewById(R.id.rule_category2);
						my_spinner.setVisibility(View.VISIBLE);
						
						TextView textview = (TextView)findViewById(R.id.rule_than);
						textview.setVisibility(View.VISIBLE);
						
					} else {
						my_spinner = (Spinner)findViewById(R.id.rule_qualifier2);
						my_spinner.setVisibility(View.GONE);
						
						my_spinner = (Spinner)findViewById(R.id.rule_quantity2);
						my_spinner.setVisibility(View.GONE);
						
						my_spinner = (Spinner)findViewById(R.id.rule_category2);
						my_spinner.setVisibility(View.GONE);
						
						TextView textview = (TextView)findViewById(R.id.rule_than);
						textview.setVisibility(View.GONE);
					}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
    		
    	};
    	
    	spinner.setOnItemSelectedListener(itemListener);

		spinner = (Spinner)this.findViewById(R.id.rule_qualifier1);  
		adapter = ArrayAdapter.createFromResource(this, R.array.rule_qualifiers, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spinner.setAdapter(adapter);
		
		spinner = (Spinner)this.findViewById(R.id.rule_quantity1);  
		adapter = ArrayAdapter.createFromResource(this, R.array.rule_0_10, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spinner.setAdapter(adapter);
		
		spinner = (Spinner)this.findViewById(R.id.rule_category1);  
		adapter = ArrayAdapter.createFromResource(this, R.array.rule_categories, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spinner.setAdapter(adapter);
		
		spinner = (Spinner)this.findViewById(R.id.rule_qualifier2);  
		adapter = ArrayAdapter.createFromResource(this, R.array.rule_qualifiers, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spinner.setAdapter(adapter); 
		
		spinner = (Spinner)this.findViewById(R.id.rule_quantity2);  
		adapter = ArrayAdapter.createFromResource(this, R.array.rule_0_10, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spinner.setAdapter(adapter);
		
		spinner = (Spinner)this.findViewById(R.id.rule_category2);  
		adapter = ArrayAdapter.createFromResource(this, R.array.rule_categories, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spinner.setAdapter(adapter);

    }
    
    public void onClickedDone(View v) {
    	String rule[];
    	
		rule = new String[7 + 1];
    	
    	rule[0] = ((EditText)this.findViewById(R.id.name_entry)).getText().toString();
    	
    	Spinner spinner = (Spinner)this.findViewById(R.id.rule_question);
     	rule[1] = spinner.getSelectedItem().toString();
    	
		spinner = (Spinner)this.findViewById(R.id.rule_qualifier1);  
		rule[2] = spinner.getSelectedItem().toString();
		
		spinner = (Spinner)this.findViewById(R.id.rule_quantity1);  
		rule[3] = spinner.getSelectedItem().toString();
		
		spinner = (Spinner)this.findViewById(R.id.rule_category1);  
		rule[4] = spinner.getSelectedItem().toString();
		
		spinner = (Spinner)this.findViewById(R.id.rule_qualifier2);  
		rule[5] = spinner.getSelectedItem().toString();
		
		spinner = (Spinner)this.findViewById(R.id.rule_quantity2);  
		rule[6] = spinner.getSelectedItem().toString();
		
		spinner = (Spinner)this.findViewById(R.id.rule_category2);
		rule[7] = spinner.getSelectedItem().toString();
		
		for(String s: rule) {
			if(s!= null)
				Log.i("test", s);	
		}
		//TODO: check to see if that rule name exists already
		
		finish();
    }
    
}

